package com.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.domain.dto.OrderDetailDTO;
import com.mall.domain.dto.SearchForm;
import com.mall.domain.po.Item;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ItemMapper extends BaseMapper<Item> {

    @Update("UPDATE item SET stock = stock - #{num} WHERE id = #{itemId} and stock >= #{num}")
    void updateStock(OrderDetailDTO orderDetail);

    // 查询当前库存（用于验证扣减结果）
    @Select("SELECT stock FROM item WHERE id = #{itemId}")
    Integer queryStockById(@Param("itemId") Long itemId);

    List<String> selectDistinctCategories();

    List<String> selectDistinctbrands();

    IPage<Item> getItemList(SearchForm searchForm, Page<Item> itemPage);
}
